﻿/*
 	PureMVC UnifiedLoader Utility
 	Copyright (c) 2008 neil manuell
  	Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package org.puremvc.as3.multicore.utilities.unifiedloader.model{
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	import org.puremvc.as3.multicore.utilities.unifiedloader.interfaces.IAssetInfo;
	import org.puremvc.as3.multicore.utilities.unifiedloader.loader.UnifiedLoader;
	import org.puremvc.as3.multicore.utilities.unifiedloader.loader.LoaderNotificationNames;
	
	/**
	 * PureMVC Proxy which wraps the Loader Class. This provides the interface for the utility
	 */
	public class DataLoaderProxy extends Proxy{
		
		public static const NAME:String = "DataLoaderProxy";

		public function DataLoaderProxy() 
		{
			super(NAME, new UnifiedLoader());
			init()
		}
		
		/**
		 * indicates whether the load queue is active
		 */
		public function get isActive():Boolean{
			return assetLoader.isActive;
		}
		
		/**
		 * the number of items left in the load queue
		 */
		public function get queueLength():int{
			return assetLoader.queueLength;
		}
		
		/**
		 * the function that defines the load queue order (see Array.sort())
		 */
		public function set queueSortFunction(param:Function):void
		{
			assetLoader.queueSortFunction = param;
		}
		
		/**
		 * defines whether the load queue should automatically start when an item has been added
		 * @default true
		 */
		public function get autoLoad():Boolean
		{
			return assetLoader.autoLoad;
		}
		
		/**
		 * @private
		 */
		public function set autoLoad(param:Boolean):void
		{
			assetLoader.autoLoad = param;
		}

		/**
		 * returns the IAssetInfo instance associated with the url.  
		 * If one doesn't exist, it will create one.  You have to add this to the
		 * loadQueue with the addToQueue() method.
		 * @param url the url of the asset to be loaded
		 * @return the IAssetInfo instance
		 * @see org.puremvc.as3.multicore.utilities.unifiedloader.interfaces.IAssetInfo
		 * 
		 */
		public function getAssetInfo(url:String):IAssetInfo
		{
			return assetLoader.getAssetInfo(url);
		}
		
		/**
		 * checks if an IAssetInfo instance is stored in the loader
		 * @param url the url of the asset to checks
		 * @return whether the specified url has a IAssetInfo instance stored in the Loader
		 * 
		 */

		public function hasAssetInfo(url:String):Boolean
		{
			return assetLoader.hasAssetInfo(url);
		}
		
		/**
		 * indicates the load state of the IAssetInfo indicated by the url. 
		 * @param url the url associated with the IAssetInfo whose status is to be retrieved
		 * @return the load status of the IAssetInfo associated with the url
		 * @see org.puremvc.as3.multicore.utilities.unifiedloader.etypes.AssetInfoStatus
		 * 
		 */
		public function getStatus(url:String):String
		{
			return assetLoader.getStatus(url);
		}
		
		/**
		 * deletes the IAssetInfo associated with the url from the internal registery.
		 * @param url the url associated with the IAssetInfo to be deleted
		 * 
		 */
		public function deleteAssetInfo(url:String):void
		{
			assetLoader.deleteAssetInfo(url)
		}
		
		/**
		 * This adds an IAssetInfo to the load queue. The IAssetInfo must first be
		 * aquired from the getAsset() method.
		 * @param param the IAssetInfo instance to add to the load queue
		 * @see #getAsset
		 * @see org.puremvc.as3.multicore.utilities.unifiedloader.interfaces.IAssetInfo
		 * 
		 */
		public function addToQueue(param:IAssetInfo):void{
			
			assetLoader.addToQueue(param)
		}
		
		/**
		 * Stops the load queue
		 * 
		 */
		public function stopQueue():void{
			
			assetLoader.stopQueue();
			
		}
		/**
		 * Starts the load queue 
		 * 
		 */
		public function startQueue():void{
			
			assetLoader.startQueue();
		}
		
		/**
		 * flushes the load queue
		 * 
		 */
		public function flushQueue():void
		{	
			assetLoader.flushQueue()	
			
		}
		
		/**
		 * removes all IAssetInfo instances from the internal registery and flushes the load queue
		 * 
		 */
		public function flushAllAssets():void
		{			
			assetLoader.flushAllAssets();
			

		}
		
		/**
		 * removes all IAssetInfo instances from the internal registery, and clears all registered clients, 
		 * and flushes the load queue
		 * 
		 */
		public function deconstruct():void
		{			
			
			assetLoader.deconstruct();
			

		}
		
		/**
		 * Registers a client with the Loader along with the id that maps to it.
		 * The clientType property of an IAssetInfo must be set to this before adding it to the queue
		 * @param id the id that maps to the client
		 * @param c the client as a class (must extend LoaderClientBase)
		 * @see org.puremvc.as3.multicore.utilities.unifiedloader.clients.LoaderClientBase
		 * 
		 */
		public function registerClient(id:String, c:Class):void
		{
			assetLoader.registerClient(id, c)
		}
		
		/**
		 * Removes the client mapped with the id 
		 * @param id the id that maps to the client
		 * 
		 */
		public function removeClient(id:String):void
		{
			assetLoader.removeClient(id);
		}
		
		private function init():void
		{
			assetLoader.addEventListener(LoaderNotificationNames.ITEM_COMPLETED, relayEvent)
			assetLoader.addEventListener(LoaderNotificationNames.ITEM_STARTED, relayEvent)
			assetLoader.addEventListener(LoaderNotificationNames.QUEUE_COMPLETED, relayEvent)
			assetLoader.addEventListener(LoaderNotificationNames.QUEUE_PROGRESS, relayEvent)
			assetLoader.addEventListener(LoaderNotificationNames.ITEM_IOERROR, relayEvent)
		}
		
		private function get assetLoader():UnifiedLoader{
			return UnifiedLoader(getData());
		}
		
		private function relayEvent(event:Event):void
		{
			sendNotification(event.type, event)
		}
		
	}
	
}


	
	
	
	
	